package com.mrd.permissions;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

/**
 * 线程池异步处理提交任务
 * 
 * @author han
 */
@Component
public class FutureThreadPool implements InitializingBean {
  public static final int THREADPOOL_SIZE = 15;
  private static ExecutorService threadExecutor;

  /**
   * 线程池处理Callable<T>,FutureTask<T> 类型，有返回
   * 
   * @author han
   * @param callable
   * @return
   */
  public <T> FutureTask<T> executeTask(Callable<T> callable) {
    FutureTask<T> futureTask = new FutureTask<T>(callable);
    threadExecutor.submit(futureTask);
    return futureTask;
  }


  @Override
  public void afterPropertiesSet() throws Exception {
    threadExecutor = Executors.newFixedThreadPool(THREADPOOL_SIZE);
  }
}
